#include "newgame.h"
#include "ui_newgame.h"
#include <QtDebug>
#include "startsettings.h"
#include <qmessagebox.h>
#include "vsserver.h"
#include "talkdialog.h"
#include "vssocket.h"
#include "mainwindow.h"
NewGame::NewGame(QWidget *parent) :
    QDialog(parent),
	ui(new Ui::NewGame),server(0),socket(0)
{
    ui->setupUi(this);
	connect(ui->cancel,SIGNAL(clicked()),this,SLOT(close()));
	if(StartSettings::socket)
	{
		delete StartSettings::socket;
		StartSettings::socket=0;
	}
	if(StartSettings::server)
	{
		delete StartSettings::server;
		StartSettings::server=0;
	}
}

NewGame::~NewGame()
{
    delete ui;
}

void NewGame::on_single_toggled(bool checked)
{
	ui->singleblack->setEnabled(checked);
	ui->singlewhite->setEnabled(checked);
	ui->DifficultySlider->setEnabled(checked);
}

void NewGame::on_network_toggled(bool checked)
{
	ui->netblack->setEnabled(checked && ui->server->isChecked());
	ui->netwhite->setEnabled(checked && ui->server->isChecked());
	ui->con->setEnabled(checked && !ui->server->isChecked());
	ui->ipadd->setEnabled(checked && !ui->server->isChecked());
	ui->port->setEnabled(checked);
	ui->server->setEnabled(checked);
	ui->nickName->setEnabled(checked);
	ui->ok->setEnabled(!checked);
}

void NewGame::on_singleblack_clicked()
{
	ui->singleblack->setChecked(1);
	ui->singlewhite->setChecked(0);
}

void NewGame::on_singlewhite_clicked()
{
	ui->singleblack->setChecked(0);
	ui->singlewhite->setChecked(1);
}

void NewGame::on_netblack_clicked()
{
	ui->netblack->setChecked(1);
	ui->netwhite->setChecked(0);
}

void NewGame::on_netwhite_clicked()
{
	ui->netblack->setChecked(0);
	ui->netwhite->setChecked(1);
}

void NewGame::on_server_toggled(bool checked)
{
	ui->ipadd->setEnabled(!checked);
	//ui->con->setEnabled(!checked);
	ui->netblack->setEnabled(checked);
	ui->netwhite->setEnabled(checked);
	//ui->ok->setEnabled(checked);
	if(checked)
	{
		ui->con->setText(tr("Run"));
	}else
		ui->con->setText(tr("Connect"));
}


void NewGame::on_ok_clicked()
{
	if(ui->single->isChecked())
	{
		StartSettings::gameType=StartSettings::singleGame;
		StartSettings::playerColor=ui->singleblack->isChecked();
	}else if(ui->twoplayers->isChecked())
	{
		StartSettings::gameType=StartSettings::twoplayers;
		StartSettings::playerColor=1;
	}else if(ui->network->isChecked())
	{

		StartSettings::gameType=StartSettings::netGame;
		StartSettings::isServer=ui->server->isChecked();
		if(StartSettings::isServer)
		{
			if(!StartSettings::server)
				StartSettings::server=server;
			StartSettings::playerColor=ui->netblack->isChecked();
			StartSettings::socket->sendStartMessage(1-StartSettings::playerColor);
		}else
		{
			if(StartSettings::socket)
			{
				delete StartSettings::socket;
				StartSettings::socket=0;
			}
			StartSettings::socket=socket;
			StartSettings::server=0;
		}
		server=0;socket=0;
		StartSettings::talkDialog->show();
		StartSettings::talkDialog->updateName();
	}

	emit start();
	close();
}

void NewGame::on_con_clicked()
{
	bool ok;
	int port=ui->port->text().toInt(&ok);
	if(!ok)
	{
		QMessageBox::warning(this,tr("error"),tr("Port is wrong"));
		return;
	}
	if(ui->nickName->text().isEmpty())
	{
		QMessageBox::warning(this,tr("error"),tr("Plase input nickname"));
		return;
	}
	StartSettings::nickName=ui->nickName->text();
	ui->con->setEnabled(0);
	if(ui->server->isChecked())
	{
		//StartSettings::gameType=StartSettings::netGame;
		if(server)
		{
			delete server;
			server=0;
		}
		server = new VSServer(StartSettings::mainWindow);
		if(!server->listen(QHostAddress::Any,port))
		{
			addMsg(tr("Cannot creat server.\n"));
			//emit error();
			//close();
			return;
		}

		addMsg(tr("Succeed creating server.\n"));
		connect(server,SIGNAL(ready()),this,SLOT(onready()));
		connect(server,SIGNAL(newConnection()),this,SLOT(newCon()));
		connect(server,SIGNAL(sendMsg(QString)),this,SLOT(addMsg(QString)));
		//StartSettings::talkDialog->show();
	}else
	{
		if(socket)
		{
			delete socket;
			socket=0;
		}
		socket = new VSSocket(StartSettings::mainWindow);
		connect(socket,SIGNAL(connected()),this,SLOT(connected()));
		connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(onerror()));
		connect(socket,SIGNAL(erred(QString)),this,SLOT(addMsg(QString)));
		connect(socket,SIGNAL(sendMsg(QString)),this,SLOT(addMsg(QString)));
		connect(socket,SIGNAL(ready()),this,SLOT(onready()));
		connect(socket,SIGNAL(started()),this,SLOT(on_ok_clicked()));
		socket->connectToHost(ui->ipadd->text(),port);
		//addMsg("connecting to ");
		//addMsg(ui->ipadd->text());
		//addMsg(":");
		//addMsg(ui->port->text());
		QString s;
		s.sprintf("Connecting to %s:%s\n",ui->ipadd->text().toLatin1().data(),ui->port->text().toLatin1().data());
		addMsg(s);

	}
}
void NewGame::addMsg(const QString s)
{
	msg+=s;
	ui->message->setText(msg);
}
void NewGame::onready()
{
	if(ui->server->isChecked())
	{
		ui->ok->setEnabled(1);
		connect(StartSettings::socket,SIGNAL(talked(QString)),StartSettings::talkDialog,SLOT(addMsg(QString)));
	}else
	{
		connect(socket,SIGNAL(talked(QString)),StartSettings::talkDialog,SLOT(addMsg(QString)));
	}

	//addMsg("ready\n");
	//qDebug()<<"Net game ready";
}
void NewGame::newCon()
{
	addMsg(tr("New player connected\n"));
}
void NewGame::onerror()
{
	addMsg(tr("connection error\n"));
	ui->con->setEnabled(1);
}
void NewGame::connected()
{
	addMsg(tr("connected\n"));
	socket->sendWelcomeMessage();
}
